package com.adcc.loadsheet.monitor.service;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.monitor.entity.Remark;
import com.adcc.loadsheet.monitor.util.*;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统配置服务层
 */
@Service("SystemConfigService")
public class SystemConfigServiceImpl implements SystemConfigService {

    @Autowired
    private SystemConfigFactory systemConfigFactory;

    @Autowired
    private AppUploadUDPFactory appUploadUDPFactory;

    @Autowired
    private MsgUpCenterUDPFactory msgUpCenterUDPFactory;

    @Override
    public Map<String, Object> getSystemConfig(String configType) throws Exception {
        try {
            Map<String, Object> resultMap = Maps.newHashMap();
            switch (configType) {
                case Constant.CONFIG_BGS:
                    resultMap = systemConfigFactory.getBgsConfigMap();
                    break;
                case Constant.CONFIG_DATABASE:
                    resultMap = systemConfigFactory.getDatabaseConfigMap();
                    break;
                case Constant.CONFIG_MQ:
                    resultMap = systemConfigFactory.getMqConfigMap();
                    break;
                case Constant.CONFIG_APP_UPLOAD:
                    resultMap = systemConfigFactory.getAppUploadConfigMap();
                    break;
                case Constant.CONFIG_AIR_INFO:
                    resultMap = systemConfigFactory.getAirInfoConfigMap();
                    break;
                case Constant.CONFIG_MSG_UP_CENTER:
                    resultMap = systemConfigFactory.getMsgUpCenterConfigMap();
                    break;
                case Constant.CONFIG_WEBSITE:
                    resultMap = systemConfigFactory.getWebsiteConfigMap();
                    break;
                case Constant.CONFIG_FORWARDER:
                    resultMap = systemConfigFactory.getForwarderConfigMap();
                    break;
                case Constant.IGNOREALARM:
                    resultMap = systemConfigFactory.getIgnoreAlarmConfigMap();
                    break;
                case Constant.ROLEDISABLE:
                    resultMap = systemConfigFactory.getRoleDisable();
                    break;
                default:
                    break;
            }
            return resultMap;
        } catch (Exception ex) {
            Log.error(SystemConfigService.class, "getSystemConfig() errors", ex);
            throw ex;
        }
    }

    @Override
    public void saveRemark(String configType, Map<String, Object> configMap) throws Exception {
        try {
        //匹配系统配置项，并且从内存中取出除了remark的数据放到新配置中
        switch (configType){
            case Constant.CONFIG_BGS:
                // 只更新BGS模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getBgsConfigMap().put("remark",configMap.get("remark"));
                systemConfigFactory.saveBgsConfig(systemConfigFactory.getBgsConfigMap());
                break;
            case Constant.CONFIG_DATABASE:
                // 只更新数据库模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getDatabaseConfigMap().put("remark",configMap.get("remark"));
                systemConfigFactory.saveDatabaseConfig(systemConfigFactory.getDatabaseConfigMap());
                break;
            case Constant.CONFIG_MQ:
                // 只更新MQ模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getMqConfigMap().put("remark",configMap.get("remark"));
                systemConfigFactory.saveMQConfig(systemConfigFactory.getMqConfigMap());
                break;
            case Constant.CONFIG_APP_UPLOAD:
                // 只更新appupload模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getAppUploadConfigMap().put("remark",configMap.get("remark"));
                configMap = new HashMap<>(systemConfigFactory.getAppUploadConfigMap());
                // 重新加载AppUploadUDPFactory配置
                appUploadUDPFactory.loadAppUploadConfig(systemConfigFactory.getAppUploadConfigMap());
                systemConfigFactory.saveAppUploadConfig(configMap);
                break;
            case Constant.CONFIG_AIR_INFO:
                // 只更新舱单数据模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getAirInfoConfigMap().put("remark",configMap.get("remark"));
                systemConfigFactory.saveAirInfoConfig(systemConfigFactory.getAirInfoConfigMap());
                break;
            case Constant.CONFIG_MSG_UP_CENTER:
                // 只更新MsgUpCenter模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getMsgUpCenterConfigMap().put("remark",configMap.get("remark"));
                configMap = new HashMap<>(systemConfigFactory.getMsgUpCenterConfigMap());
                // 重新加载MsgUpCenterUDPFactory配置
                msgUpCenterUDPFactory.loadMsgUpCenterConfig(systemConfigFactory.getMsgUpCenterConfigMap());
                systemConfigFactory.saveMsgUpCenterConfig(configMap);
                break;
            case Constant.CONFIG_WEBSITE:
                // 只更新地服网站模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getWebsiteConfigMap().put("remark",configMap.get("remark"));
                systemConfigFactory.saveWebsiteConfig(systemConfigFactory.getWebsiteConfigMap());
                break;
            case Constant.CONFIG_FORWARDER:
                // 只更新舱单抓取程序模块Remark,防止与系统配置修改发生覆盖
                systemConfigFactory.getForwarderConfigMap().put("remark",configMap.get("remark"));
                systemConfigFactory.saveForwarderConfig(systemConfigFactory.getForwarderConfigMap());
                break;
            default:
                break;
            }
            Remark remark = mapToRemark(configMap);
            //保存成功记录系统日志和操作日志
            Log.info(Constant.OPERATION_LOG,String.format(Constant.SAVE_SUCCESS_ENGLISH, configType));
            Log.info(SystemConfigService.class, String.format(Constant.SAVE_SUCCESS_SYSTEM, configType,remark.toString()));
        } catch (Exception ex) {
            //保存失败
            Log.error(Constant.OPERATION_LOG,String.format(Constant.SAVE_ERROR_ENGLISH, configType,ex));
            Log.error(SystemConfigService.class, "saveRemark() errors", ex);
            throw ex;
        }
    }

    @Override
    public void save(String configType, Map<String, Object> configMap) throws Exception {
        try {
            //匹配系统配置项，并且从内存中取出remark数据放到新配置中
            switch (configType) {
                case Constant.CONFIG_BGS:
                    // 不更新BGS模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getBgsConfigMap().get("remark"));
                    systemConfigFactory.saveBgsConfig(configMap);
                    break;
                case Constant.CONFIG_DATABASE:
                    // 不更新数据库块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getDatabaseConfigMap().get("remark"));
                    systemConfigFactory.saveDatabaseConfig(configMap);
                    break;
                case Constant.CONFIG_MQ:
                    // 不更新MQ模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getMqConfigMap().get("remark"));
                    systemConfigFactory.saveMQConfig(configMap);
                    break;
                case Constant.CONFIG_APP_UPLOAD:
                    // 不更新appUpload模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getAppUploadConfigMap().get("remark"));
                    // 保存AppUpload配置
                    systemConfigFactory.saveAppUploadConfig(configMap);
                    // 重新加载AppUploadUDPFactory配置
                    appUploadUDPFactory.loadAppUploadConfig(systemConfigFactory.getAppUploadConfigMap());
                    // 重启AppUploadUDPFactory
                    appUploadUDPFactory.restart();
                    break;
                case Constant.CONFIG_AIR_INFO:
                    // 不更新舱单数据模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getAirInfoConfigMap().get("remark"));
                    systemConfigFactory.saveAirInfoConfig(configMap);
                    break;
                case Constant.CONFIG_MSG_UP_CENTER:
                    // 不更新MsgUpCenter模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getMsgUpCenterConfigMap().get("remark"));
                    // 保存MsgUpCenter配置
                    systemConfigFactory.saveMsgUpCenterConfig(configMap);
                    // 重新加载MsgUpCenterUDPFactory配置
                    msgUpCenterUDPFactory.loadMsgUpCenterConfig(systemConfigFactory.getMsgUpCenterConfigMap());
                    // 重启MsgUpCenterFactory
                    msgUpCenterUDPFactory.restart();
                    break;
                case Constant.CONFIG_WEBSITE:
                    // 不更新地服网站模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getWebsiteConfigMap().get("remark"));
                    systemConfigFactory.saveWebsiteConfig(configMap);
                    break;
                case Constant.CONFIG_FORWARDER:
                    // 不更新舱单抓取程序模块Remark,防止与系统配置修改发生覆盖
                    configMap.put("remark", systemConfigFactory.getForwarderConfigMap().get("remark"));
                    systemConfigFactory.saveForwarderConfig(configMap);
                    break;
                case Constant.IGNOREALARM:
                    systemConfigFactory.saveIgnoreAlarm(configMap);
                    break;
                case Constant.ROLEDISABLE:
                    systemConfigFactory.saveRoleDisable(configMap);
                    break;
                default:
                    break;
            }
        } catch (Exception ex) {
            Log.error(SystemConfigService.class, "save() errors", ex);
            throw ex;
        }
    }

    /**
     * map转成Remark对象
     */
    private  Remark mapToRemark(Map<String, Object> map) {
        if (map.containsKey("remark")) {
            Map<String, String> mapre = (Map<String, String>) map.get("remark");
            Remark remark = new Remark();
            remark.setPersonnel(mapre.get("personnel"));
            remark.setEmergencyplans(mapre.get("emergencyplans"));
            return remark;
        }
        return null;
    }
}
